/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;


/**
 *
 * @author Edu
 */
@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    private String nombre=null;
    private String categoria=null;
    private String precio=null;
    private String descripcion=null; 
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    Connection conn = null;
    public void init(ServletConfig config) throws ServletException {
    super.init(config);

    String bbddString = new String("jdbc:derby://localhost:1527/tienda_edomingo;create=true");

    try{
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    }catch(ClassNotFoundException ex){
        System.out.println("Error al cargar el driver");
        System.out.println(ex.getMessage());
    }

    try{
        conn = DriverManager.getConnection(bbddString);
    }catch (SQLException sqlEx){
        System.out.println("Error al establecer la conexión ");
        System.out.println(sqlEx.getMessage());
    }
        System.out.println("Iniciando ServletOpinion (versión BD)..");
    }

     
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        processRequest(req, resp);
        try {
            devolverPaginaHTML(req,resp);
        } catch (SQLException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

        public void devolverPaginaHTML(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException{
            

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String pass;
        pass = req.getParameter("pass");
        Statement s = conn.createStatement();
        ResultSet rs = s.executeQuery ("select * from producto");       
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            if(pass.equals("1234")){
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet login</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Bienvenida Paqui</h1>");
            while (rs.next())
            {
                out.println (rs.getString(2));
                out.println("<br>");
            }
            out.println("</body>");
            out.println("</html>");
            }
            else{
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet login</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Contraseña errónea</h1>");
            out.println("</body>");
            out.println("</html>");  
            }
            
        } finally {            
            out.close();
        }

        out.flush();
        out.close();
       // conn.close();
        } 
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    /*@Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public void destroy() {
        super.destroy();
        System.out.println("Cerrando conexión...");
    try{
        conn.close();
    }catch(SQLException ex){
        System.out.println("No se pudo cerrar la conexión");
        System.out.println(ex.getMessage());
    }
}
}*/
